package com.holly.unit.genx.listener;


import cn.hutool.core.util.StrUtil;
import com.holly.unit.core.enums.DbTypeEnum;
import com.holly.unit.core.listener.ContextInitializedListener;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
/**
 * 类描述: 代码生成器初始化数据
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/17 13:57
 */
public class GenCodeInitListener extends ContextInitializedListener implements Ordered {

    @Override
    public void eventCallback(ApplicationContextInitializedEvent event) {

        ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();


        // GenCode的配置
        String enabledStr = environment.getProperty("gen.code.config.enabled");
        String dbType = environment.getProperty("gen.code.config.dbType");
        String diverName = environment.getProperty("gen.code.config.diverName");
        String url = environment.getProperty("gen.code.config.url");
        String username = environment.getProperty("gen.code.config.username");
        String password = environment.getProperty("gen.code.config.password");

        String dbName = environment.getProperty("gen.code.config.dbName");
        String projectPath = environment.getProperty("gen.code.config.projectPath");
        String bussiPackage = environment.getProperty("gen.code.config.bussiPackage");
        String srcPackage = environment.getProperty("gen.code.config.srcPackage").replace(".","/");;
        String webPackage = environment.getProperty("gen.code.config.webPackage").replace(".","/");;
        String templatePath = environment.getProperty("gen.code.config.templatePath");

        String isHump = environment.getProperty("gen.code.config.isHump");
        String dbTableId = environment.getProperty("gen.code.config.dbTableId");
        String pageFieldReqNum = environment.getProperty("gen.code.config.pageFieldReqNum");
        String pageQueryNum = environment.getProperty("gen.code.config.pageQueryNum");
        String pageFilterFields = environment.getProperty("gen.code.config.pageFilterFields");
        String fieldInRowNum = environment.getProperty("gen.code.config.fieldInRowNum");

        String type = dbType(url);


        // 是否开启gencode，默认false.
        boolean enabled = false;
        if (StrUtil.isNotBlank(enabledStr)) {
            enabled = Boolean.parseBoolean(enabledStr);
        }

        // 直接return
        if (!enabled) {
            return;
        }

    }

    @Override
    public int getOrder() {
        return 0;
    }


    /***
     * 根据URL判断数据库类型
     * @param url
     * @return
     */
    private static String dbType(String url) {
        String res = "";
        if (url.indexOf("mysql")>=0 || url.indexOf("MYSQL")>=0) {
            res = DbTypeEnum.MYSQL.getUrlWords();
        } else if (url.indexOf("oracle")>=0 || url.indexOf("ORACLE")>=0) {
            res = DbTypeEnum.ORACLE.getUrlWords();
        } else if (url.contains(DbTypeEnum.MS_SQL.getUrlWords())){
            res = DbTypeEnum.MS_SQL.getUrlWords();
        } else if (url.indexOf("postgresql")>=0 || url.indexOf("POSTGRESQL")>=0) {
            res = DbTypeEnum.PG_SQL.getUrlWords();
        } else {
            res = DbTypeEnum.MYSQL.getUrlWords();
        }

        return res;
    }
}
